﻿using System;
using System.Collections.Generic;

public class ObjData
{
    /// <summary>
    /// 配置信息id
    /// </summary>
    public int objId;
    /// <summary>
    /// 字符串替换
    /// </summary>
    public string[] strArg;
    /// <summary>
    /// 按钮回调函数
    /// </summary>
    public Action[] callArg;

    public ObjData(int objId, string[] strArg, Action[] callArg)
    {
        this.objId = objId;
        this.strArg = strArg;
        this.callArg = callArg;
    }
}

public class FaceCenter
{
    Dictionary<Type, CSIMediator> mCSDict = new Dictionary<Type, CSIMediator>();
    Dictionary<int, LuaIMediator> mLuaDict = new Dictionary<int, LuaIMediator>();
    public FaceCenter()
    {
        ReconnectMgr.GetSingleton().Close();
        LoadingMgr.GetSingleton();
        RegisterSceneOpen();
    }

    public T GetCSIMediator<T>() where T : CSIMediator
    {
        CSIMediator mdt = null; Type type = typeof(T);
        mCSDict.TryGetValue(type, out mdt);
        if (mdt == null)
        {
            mdt = Activator.CreateInstance(type) as CSIMediator;
            mCSDict.Add(type, mdt); ZMgr.UIMgr.Register(mdt);
        }
        return mdt as T;
    }

    public LuaIMediator GetLuaIMediator(int mdtId)
    {
        LuaIMediator mdt = null;
        mLuaDict.TryGetValue(mdtId, out mdt);
        return mdt;
    }

    public void AddLuaIMediator(LuaIMediator mdt)
    {
        mLuaDict[mdt.PanelId] = mdt;
        ZMgr.UIMgr.Register(mdt);
    }

    private void RegisterSceneOpen()
    {
        RegisterSceneOpen(SceneName.LEVEL_3, PanelID.MainMenuPanel);
        RegisterSceneOpen(SceneName.LEVEL_3, PanelID.DanhangPanel);
        
        if (ZMgr.GuideMgr.IsFinishGuideFuben())
        {
            RegisterSceneOpen(SceneName.LEVEL_2, PanelID.LoginPanel);
        }
        else
        {
            LoadBoardMgr.GetSingleton().Close();
            ZEventCenter.DispatchEvent(new ZEvent(ConstData.GUIDE_ENTER_FUBEN));
        }
    }

    public void OpenDialog(int objId, string[] strArg = null, Action[] callArg = null)
    {
        ObjData objData = new ObjData(objId, strArg, callArg);
        OpenUIView(PanelID.DialogBasePanel, objData);
    }

    public void OpenErrorNormal(int objId, string[] strArg = null)
    {
        ObjData objData = new ObjData(objId, strArg, null);
        OpenUIView(PanelID.ErrorNormalPanel, objData);
    }

    public void OpenHelpWord(int objId, string[] strArg = null, Action[] callArg = null)
    {
        ObjData objData = new ObjData(objId, strArg, callArg);
        OpenUIView(PanelID.HelpWordPanel, objData);
    }

    public void OpenGuideDialog(int dialogId, bool isGuide)
    {
        OpenUIView(PanelID.GuideDialogPanel, dialogId, isGuide);
    }

    public void CloseGuideDialog()
    {
        CloseUIView(PanelID.GuideDialogPanel);
    }

    public void OpenGuide(float delayTime)
    {
        OpenUIView(PanelID.GuidePanel, delayTime);
    }

    /// <summary>
    /// 返回到登陆界面
    /// </summary>
    public void LogOutToLoginView()
    {
        PlayerData.GetSingleton().roleAttr.Clear();
        PlayerData.GetSingleton().ReconnectInitData();

        CloseUIView(PanelID.LoginPanel);
        CloseUIView(PanelID.DanhangPanel);
    }

    /// <summary>
    /// 打开界面
    /// </summary>
    /// <param name="key">界面UId</param>
    /// <param name="args">界面参数</param>
    public void OpenUIView(PanelID key, params object[] args)
    {
        CSIMediator mdt = GetCtrlByID(key);
        if (mdt != null)
        {
            mdt.SetData(args);
            ZMgr.UIMgr.Open(mdt.PanelId);
        }
    }

    /// <summary>
    /// 关闭界面
    /// </summary>
    /// <param name="key">界面UId</param>
    public void CloseUIView(PanelID key)
    {
        CSIMediator mdt = GetCtrlByID(key);
        if (mdt != null)
        {
            ZMgr.UIMgr.Close((int)key);
        }
    }
    
    /// <summary>
    /// 通过key获取mdt
    /// </summary>
    /// <param name="key"></param>
    public CSIMediator GetCtrlByID(PanelID key)
    {
        CSIMediator mdt = null;
        switch (key)
        {
            case PanelID.CmdPanel:
                mdt = GetCSIMediator<GMToolsCtrl>();
                break;
            case PanelID.GuideDialogPanel:
                mdt = GetCSIMediator<GuideDialogCtrl>();
                break;
            case PanelID.DialogBasePanel:
                mdt = GetCSIMediator<DialogBaseCtrl>();
                break;
            case PanelID.ErrorNormalPanel:
                mdt = GetCSIMediator<ErrorNormalCtrl>();
                break;
            case PanelID.HelpWordPanel:
                mdt = GetCSIMediator<HelpWordCtrl>();
                break;
            case PanelID.GuidePanel:
                mdt = GetCSIMediator<GuideCtrl>();
                break;
        }
        return mdt;
    }
    
    /// <summary>
    /// 注册绑定场景的 界面
    /// </summary>
    /// <param name="sceneName"></param>
    /// <param name="key"></param>
    public void RegisterSceneOpen(string sceneName, PanelID key)
    {
        CSIMediator mdt = GetCtrlByID(key);
        if (mdt != null)
        {
            ZMgr.UIMgr.RegisterSceneOpen(sceneName, mdt);
        }
    }

    // c#方法
    public void AddNextSceneOpenC(PanelID key)
    {
        CSIMediator mdt = GetCtrlByID(key);
        if (mdt != null)
        {
            ZMgr.UIMgr.BindSceneOpen(mdt.PanelId, true);
        }
    }

    // lua方法
    public void AddNextSceneOpenL(PanelID key)
    {
        LuaIMediator mdt = GetLuaIMediator((int)key);
        if (mdt != null)
        {
            ZMgr.UIMgr.BindSceneOpen(mdt.PanelId, true);
        }
    }
}